import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.ArrayList;

public class Grav {

	Point loc; //tiles
	Point center;
	Point playerLoc;

	int activationR;
	int activationRS; //activatino squared

	int pull; //push pull, 0 or 1
	int typeMultiplier; // 1 or -1 for pull or push gravs
	double fx, fy;

	double k = 1;
	//f = k Mass / distance squared

	boolean actingOnPlayer;
	double strength;

	public Grav( Point loc, int activationR, int pull /*wanna-be boolean*/,int strength){
		this.activationR = activationR;
		this.activationRS = activationR*activationR;
		this.pull = pull;
		this.loc = loc;

		//Gravs are centered at the intersection coordinates
		this.center = new Point( loc.x*40, loc.y*40);
		typeMultiplier = (pull==1)? 1:-1;
		//makes strength a multiplyer of 10 so that I can dampen the force
		this.strength=strength/10.0;

	}		

	public void update(Player player){		
		playerLoc = player.getRoundedCenter();
		
		double[]forces = calcForceOn(playerLoc);
		fx = forces[0]; fy = forces[1];
		
		
	}

	public double[] /* [fx,fy] */ calcForceOn(Point loc){
		double dS;

		if( (dS = loc.distanceSq(center.x, center.y)) > activationRS){
			return new double[]{0,0};

		}else{

			double angleToPoint = Math.atan2(loc.x - center.x, center.y - loc.y) ;

			if( dS < 1) dS = 1;
			k = strength * activationR/4 * 10000 / dS;
			if( k > 4000) k = 4000;

			return new double[]{
					typeMultiplier*-Math.sin(angleToPoint) * k * 1, // *1 due to gravpush/pull's mass
					typeMultiplier*Math.cos(angleToPoint) * k * 1
			};
		}
	}


	public void reset(){
		fx = fy = 0;
	}


	public Point getLocation(){
		return loc;
	}
	public Point getCenterPoint(){
		return center;
	}



}
